
//
//  RechargeListViewController.swift
//  AreaChainblock
//
//  Created by 赵佟越 on 2018/9/25.
//  Copyright © 2018年 zhaotongyue. All rights reserved.
//

import UIKit
import SwiftyJSON
import SwiftyUserDefaults
let ScreenWidth  = UIScreen.main.bounds.width
let ScreenHeight = UIScreen.main.bounds.height

class RechargeListViewController: BaseViewController ,UICollectionViewDataSource, UICollectionViewDelegate{
    var collectionView : UICollectionView?
    let Identifier       = "CollectionViewCell"
    var myModel = firstViewModel()
    let headerIdentifier = "CollectionHeaderView"
    let footIdentifier   = "CollectionFootView"
    var dataArr : NSMutableArray = [productModel.self]
    override func viewWillAppear(_ animated: Bool) {
        self.navigationController?.setNavigationBarHidden(false, animated: true)
        getData1()
    }
    
    //为白色视图添加子视图
    @objc func forgetPassworld11(){
       let theViewControllerYouSee = UIViewController.currentViewController()
        
        
        let VC = googleCodeViewController()
        VC.myCode = Defaults[.qrCodeUrl];
        theViewControllerYouSee?.navigationController?.pushViewController(VC, animated: true)
        
    }
    @objc func forgetPassworld(){
//        self.passwordView.hide();
        let theViewControllerYouSee =
            UIViewController.currentViewController()
        theViewControllerYouSee?.navigationController?.pushViewController(googleChangePayPassWorldViewController(), animated: true)
        
    }
    
    func getData() {
        MyNetwork.request(target: .goods_list(page: "0", limit: "10"), success: { (result) in
            kLog(result)
            let needModel = BaseModel()
            needModel.yy_modelSet(with: result as! [AnyHashable : Any])
            
            if needModel.code == 1111{
                //(result as AnyObject)["data"]
                
                
                var tempArr = NSMutableArray.yy_modelArray(with: productModel.self, json: needModel.mydata!["data"]!)
                //tempArr = NSArray.yy_modelArray(with: productModel.self, json: needModel.mydata!["data"] as Any)! as NSArray
                for element in tempArr! {
                    print(element)
                    self.dataArr.add(element)
                }
                
               self.collectionView?.reloadData()
                
                
            }else{
                self.navigationController?.view.makeToast(XALanguageManager.getString(key: "失败"))
            }
            
        }) { (error) in
            //kLog(error.localizedDescription)
            self.navigationController?.view.makeToast(error.localizedDescription)
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        NotificationCenter.default.addObserver(self, selector: #selector(self.forgetPassworld), name: NSNotification.Name(rawValue:"forgetPassWord"), object: nil)
        
        NotificationCenter.default.addObserver(self, selector: #selector(self.forgetPassworld11), name: NSNotification.Name(rawValue:"NoforgetPassWord"), object: nil)
        
        self.dataArr = NSMutableArray.init()
        self.getData()
        self.navigationItem.title = XALanguageManager.getString(key: "Recharge")
        
        self.view.backgroundColor = UIColor.white
        // 初始化
        let layout = UICollectionViewFlowLayout.init()
        layout.itemSize = CGSize(width: kScreenWidth - 30, height: 120)
        layout.minimumLineSpacing = 15
        layout.minimumInteritemSpacing = 5
        layout.scrollDirection = .vertical
        layout.sectionInset = UIEdgeInsets.init(top: 15, left: 15, bottom: 15, right: 15)
        // 设置分区头视图和尾视图宽高
        layout.headerReferenceSize = CGSize.init(width: ScreenWidth, height: CGFloat.leastNormalMagnitude)
        layout.footerReferenceSize = CGSize.init(width: ScreenWidth, height: CGFloat.leastNormalMagnitude)
        
        collectionView = UICollectionView.init(frame: CGRect(x:0, y:0, width:ScreenWidth, height:kScreenHeight), collectionViewLayout: layout)
        collectionView?.backgroundColor = UIColor("#EEF0F7")
        collectionView?.delegate = self
        collectionView?.dataSource = self
        collectionView?.backgroundColor = UIColor.clear
        self.view.addSubview(collectionView!)
        
        // 注册cell
        collectionView?.register(UINib.init(nibName: "CollectionViewCell", bundle: nil), forCellWithReuseIdentifier: Identifier)
        // 注册headerView
        collectionView?.register(CollectionHeaderView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: headerIdentifier)
        // 注册footView
        collectionView?.register(CollectionFootView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: footIdentifier)
        
    }
    
    //    //header高度
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize.init(width: ScreenWidth, height: CGFloat.leastNormalMagnitude)
    }
    //footer高度
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
        return CGSize.init(width: ScreenWidth, height: CGFloat.leastNormalMagnitude)
    }
    
    //设定header和footer的方法，根据kind不同进行不同的判断即可
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        if kind == UICollectionElementKindSectionHeader{
            let headerView : CollectionHeaderView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: headerIdentifier, for: indexPath) as! CollectionHeaderView
            
            headerView.view.backgroundColor = UIColor.red
            headerView.label.text = "This is HeaderView"
            return headerView
        }else{
            let footView : CollectionFootView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionFooter, withReuseIdentifier: footIdentifier, for: indexPath) as! CollectionFootView
            footView.view.backgroundColor = UIColor.purple
            footView.label.text = "This is Foot"
            return footView
        }
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.dataArr.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell:CollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: Identifier, for: indexPath) as! CollectionViewCell
        //cell.bgView.backgroundColor = armColor()
        cell.model = self.dataArr[indexPath.item] as! productModel
        
        
//        cell.titleLabel.text = String(format:"%ditem",indexPath.row)
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print(indexPath.row)
    }
    
    func armColor()->UIColor{
        let red = CGFloat(arc4random()%256)/255.0
        let green = CGFloat(arc4random()%256)/255.0
        let blue = CGFloat(arc4random()%256)/255.0
//        return UIColor(red: red, green: green, blue: blue, alpha: 1.0)
        return UIColor.white
    }
    
    func getData1() {
        MyNetwork.request(target: .home(user_id: Defaults[.userID]!), success: { (result) in
            kLog(result)
            let needModel = BaseModel()
            needModel.yy_modelSet(with: result as! [AnyHashable : Any])
            
            if needModel.code == 1111{
                self.myModel.yy_modelSet(withJSON: ((result as AnyObject)["data"]as AnyObject)["data"])
                
                Defaults[.allDKE] = self.myModel.dke
                
            }
            
            
        }) { (error) in
            kLog(error.localizedDescription)
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

